﻿Public Class Acad_EAGForm

    Dim SaveData As Boolean = False
    WithEvents dtp As New DateTimePicker

    Private Sub Acad_EAGForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If LoadPeriodBox(PeriodBox) Then
            PeriodBox.SelectedValue = CurrentPeriodID
        End If

        If ToolStrip2.Items.Count < 4 Then
            ToolStrip2.Items.Add(New ToolStripControlHost(dtp))
        End If

        dtp.Value = Now

        LoadProviderBox(ProviderBox, "Acade")

    End Sub

    Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        Search_Form.WhoSentForMe = "Acad_EAGForm"
        If Search_Form.ShowDialog = Windows.Forms.DialogResult.OK Then
            With Search_Form

                Dim UPIN As Integer = .SearchResults_DataViewGrid.Item(.SearchResults_DataViewGrid.Columns("UPIN").Index, .SearchResults_DataViewGrid.CurrentRow.Index).Value.ToString

                ProviderBox.SelectedValue = UPIN

                If ProviderBox.SelectedIndex = -1 Then
                    MessageBox.Show("The provider you selected is not set as an Academy.  If this is incorrect, please contact The Store Administrator", "Not an Academy", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    ProviderBox.SelectedIndex = 0
                End If

            End With
        End If
    End Sub

    Private Sub LoadData()

        SaveData = False

        Try
            PaymentsGrid.Rows.Clear()
        Catch ex As Exception
            'the grid is already cleared
        End Try


        ProfileLabel.Text = "Profile"

        Dim sql As String

        Try
            sql = "Select * from Acad_EAG_Data where RID = (select RID from relationships where UPIN = " & ProviderBox.SelectedValue.ToString & " and lead = 1) and Period_ID = " & PeriodBox.SelectedValue.ToString
        Catch ex As Exception
            'the boxes are not ready
            Exit Sub
        End Try

        If ProviderBox.SelectedValue.ToString = "System.Data.DataRowView" Or PeriodBox.SelectedValue.ToString = "System.Data.DataRowView" Then Exit Sub

        EAGGrid.DataSource = LoadDataView(sql)

        EAGGrid.Columns("Period_ID").Visible = False
        EAGGrid.Columns("RID").Visible = False
        EAGGrid.Columns("ID").Visible = False

        EAGGrid.Columns("Amount").DefaultCellStyle.Format = "C"

        SaveData = True
    End Sub

    Private Sub LoadProfile()

        If EAGGrid.Rows.Count = 0 Then Exit Sub

        ProfileLabel.Text = EAGGrid.Item(EAGGrid.Columns("Description").Index, EAGGrid.CurrentRow.Index).Value

        SaveData = False

        Dim sql As String
        Dim ID As Integer

        ID = EAGGrid.Item(EAGGrid.Columns("ID").Index, EAGGrid.CurrentRow.Index).Value

        Try
            sql = "select Period_ID, RID, ID, The_Date as [Date], Percentage from Acad_EAG_Profiles where RID = (select RID from relationships where UPIN = " & ProviderBox.SelectedValue.ToString & " and lead = 1) and Period_ID = " & PeriodBox.SelectedValue.ToString & " and ID = " & ID

        Catch ex As Exception
            Exit Sub
        End Try

        If ProviderBox.SelectedValue.ToString = "System.Data.DataRowView" Or PeriodBox.SelectedValue.ToString = "System.Data.DataRowView" Then Exit Sub

        PaymentsGrid.DataSource = LoadDataView(sql)

        PaymentsGrid.Columns("Period_ID").Visible = False
        PaymentsGrid.Columns("RID").Visible = False
        PaymentsGrid.Columns("ID").Visible = False

        PaymentsGrid.Columns("Date").ReadOnly = True

        PaymentsGrid.Columns("Date").DefaultCellStyle.Format = "d"
        PaymentsGrid.Columns("Percentage").DefaultCellStyle.Format = "p"
        SaveData = True
    End Sub

    Private Sub ProviderBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProviderBox.SelectedIndexChanged
        LoadData()
    End Sub

    Private Sub PeriodBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PeriodBox.SelectedIndexChanged
        LoadData()
    End Sub

    Private Sub SaveRowData(ByVal Row As Integer)
        If SaveData = False Then Exit Sub

        Dim SQL As String
        Dim Description As String
        Dim Amount As Decimal
        Dim Notes As String
        Dim RID As String
        Dim ID As Integer

        If IsDBNull(EAGGrid.Item(EAGGrid.Columns("Amount").Index, Row).Value) Then
            Amount = 0
        Else
            Amount = EAGGrid.Item(EAGGrid.Columns("Amount").Index, Row).Value
        End If

        If IsDBNull(EAGGrid.Item(EAGGrid.Columns("Notes").Index, Row).Value) Then
            Notes = ""
        Else
            Notes = EAGGrid.Item(EAGGrid.Columns("Notes").Index, Row).Value
        End If

        Description = EAGGrid.Item(EAGGrid.Columns("Description").Index, Row).Value

        RID = EAGGrid.Item(EAGGrid.Columns("RID").Index, Row).Value

        ID = EAGGrid.Item(EAGGrid.Columns("ID").Index, Row).Value

        SQL = "update Acad_EAG_Data set Description = '" & RemoveBadChars(Description) & "', Amount = " & Amount & ", Notes = '" & RemoveBadChars(Notes) & "' " & _
            "where Period_ID = " & PeriodBox.SelectedValue & " and RID = '" & RID & "' and ID = " & ID

        RunNonQuery(SQL)
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        'save the current row just in case

        If EAGGrid.SelectedCells.Count <> 0 Then
            SaveRowData(EAGGrid.CurrentRow.Index)
        End If

        'ask the user for a description and then add blank record to the system

        Dim Description As String
        Dim sql As String
        Dim TheView As DataView
        Dim ID As Integer
        Dim RID As String

        Description = InputBox("Please enter a description for the EAG you want to add", "EAG Description", "")

        If Trim(Description) = "" Then Exit Sub

        'find the next max ID

        sql = "select RID, max(ID) + 1 as ID from Acad_EAG_Data where RID = (select RID from relationships where UPIN = " & ProviderBox.SelectedValue.ToString & " and lead = 1) and Period_ID = " & PeriodBox.SelectedValue.ToString & " group by RID"

        TheView = LoadDataView(sql)

        If TheView.Count = 0 Then
            ID = 1
            RID = GetRID(ProviderBox.SelectedValue)
        Else
            ID = TheView(0).Item("ID")
            RID = TheView(0).Item("RID")
        End If

        sql = "insert into Acad_EAG_Data (Period_ID, RID, ID, Description) Values (" & PeriodBox.SelectedValue & ", '" & RID & "', " & ID & ", '" & RemoveBadChars(Description) & "')"
        RunNonQuery(sql)

        LoadData()

    End Sub

   
    Private Sub EAGGrid_CellValidated(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles EAGGrid.CellValidated
        SaveRowData(e.RowIndex)
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        'save the current row just in case

        If EAGGrid.Rows.Count = 0 Then Exit Sub

        If EAGGrid.SelectedCells.Count <> 0 Then
            SaveRowData(EAGGrid.CurrentRow.Index)
        End If

        If MessageBox.Show("Are you sure you want to delete this EAG payment?", "Delete payment?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

            Dim sql As String
            sql = "delete from Acad_EAG_Data where RID = (select RID from relationships where UPIN = " & ProviderBox.SelectedValue.ToString & " and lead = 1) and Period_ID = " & PeriodBox.SelectedValue.ToString & " and ID = " & _
                EAGGrid.Item(EAGGrid.Columns("ID").Index, EAGGrid.CurrentRow.Index).Value

            RunNonQuery(sql)
            LoadData()

        End If
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        If EAGGrid.Rows.Count = 0 Then Exit Sub
        If EAGGrid.SelectedCells.Count = 0 Then Exit Sub

        If MessageBox.Show("Are you sure you want to delete the selected payment date?", "Delete payment date", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Dim Sql As String
            Dim D As DateTime
            Dim RID As String
            Dim ID As Integer

            RID = EAGGrid.Item(EAGGrid.Columns("RID").Index, EAGGrid.CurrentRow.Index).Value
            ID = EAGGrid.Item(EAGGrid.Columns("ID").Index, EAGGrid.CurrentRow.Index).Value
            D = PaymentsGrid.Item(PaymentsGrid.Columns("Date").Index, PaymentsGrid.CurrentRow.Index).Value

            Sql = "Delete from Acad_EAG_Profiles where Period_ID = " & PeriodBox.SelectedValue & " and RID = '" & RID & "' and ID = " & ID & " and The_Date = " & D.ToOADate - 2
            RunNonQuery(Sql)
            LoadProfile()
            RecalculateProfle()
            SaveProfileData()
        End If

    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        If EAGGrid.Rows.Count = 0 Then Exit Sub

        If EAGGrid.SelectedCells.Count = 0 Then Exit Sub

        Dim sql As String
        Dim RID As String
        Dim ID As Integer

        RID = EAGGrid.Item(EAGGrid.Columns("RID").Index, EAGGrid.CurrentRow.Index).Value
        ID = EAGGrid.Item(EAGGrid.Columns("ID").Index, EAGGrid.CurrentRow.Index).Value

        If PaymentsGrid.Rows.Count = 0 Then
            'add a row and add 100% (1) to the value and reload the grid
            sql = "insert into Acad_EAG_Profiles (Period_ID, RID, ID, The_Date, Percentage) Values (" & PeriodBox.SelectedValue & ", '" & RID & "', " & ID & ", " & dtp.Value.Date.ToOADate - 2 & ", 1)"
        Else
            'add a row and add 0% to the value and reload the grid
            sql = "insert into Acad_EAG_Profiles (Period_ID, RID, ID, The_Date, Percentage) Values (" & PeriodBox.SelectedValue & ", '" & RID & "', " & ID & ", " & dtp.Value.Date.ToOADate - 2 & ", 0)"
        End If

        RunNonQuery(sql)
        LoadProfile()

    End Sub

    Private Sub EAGGrid_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles EAGGrid.CellEnter
        LoadProfile()
    End Sub

    Private Sub EAGGrid_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles EAGGrid.DataError
        MessageBox.Show("The value you have entered is not correct for this data type, please try again.", "Wrong data type entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Private Sub PaymentsGrid_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles PaymentsGrid.DataError
        MessageBox.Show("The value you have entered is not correct for this data type, please try again.", "Wrong data type entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Private Sub RecalculateProfle()
        'last row is read only so if there is only 1 row then exit
        If PaymentsGrid.Rows.Count = 0 Then Exit Sub

        If PaymentsGrid.Rows.Count = 1 Then
            PaymentsGrid.Item(PaymentsGrid.Columns("Percentage").Index, 0).Value = 1
            Exit Sub
        End If

        Dim p As Decimal = 0
        Dim r As Integer

        For r = 0 To PaymentsGrid.Rows.Count - 2
            p = p + PaymentsGrid.Item(PaymentsGrid.Columns("Percentage").Index, r).Value
        Next

        PaymentsGrid.Item(PaymentsGrid.Columns("Percentage").Index, PaymentsGrid.Rows.Count - 1).Value = 1 - p

    End Sub

    Private Sub PaymentsGrid_CellValidated(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PaymentsGrid.CellValidated
        If SaveData = False Then Exit Sub

        RecalculateProfle()
        SaveProfileData()

    End Sub

    Private Sub PaymentsGrid_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PaymentsGrid.CellEnter
        If e.RowIndex = PaymentsGrid.Rows.Count - 1 Then
            PaymentsGrid.Rows(e.RowIndex).ReadOnly = True
        Else
            PaymentsGrid.Rows(e.RowIndex).ReadOnly = False
        End If
    End Sub

    Private Sub SaveProfileData()
        If SaveData = False Then Exit Sub

        If PaymentsGrid.Rows.Count = 0 Then Exit Sub

        Dim Sql As String
        Dim r As Integer
        Dim p As Decimal
        Dim D As DateTime
        Dim RID As String
        Dim ID As Integer

        RID = EAGGrid.Item(EAGGrid.Columns("RID").Index, EAGGrid.CurrentRow.Index).Value
        ID = EAGGrid.Item(EAGGrid.Columns("ID").Index, EAGGrid.CurrentRow.Index).Value

        For r = 0 To PaymentsGrid.Rows.Count - 1
            p = PaymentsGrid.Item(PaymentsGrid.Columns("Percentage").Index, r).Value
            D = PaymentsGrid.Item(PaymentsGrid.Columns("Date").Index, r).Value

            Sql = "Update Acad_EAG_Profiles set Percentage = " & p & " where Period_ID = " & PeriodBox.SelectedValue & " and RID = '" & RID & "' and ID = " & ID & " and The_Date = " & D.ToOADate - 2
            RunNonQuery(Sql)

        Next


    End Sub
End Class